﻿using System;
using System.Linq;
using System.Text;
using Caliburn.Core.Metadata;
using Caliburn.PresentationFramework.ApplicationModel;
using Microsoft.Practices.ServiceLocation;
using System.Threading;
using XWord.WPF.ApplicationFramework;
using Luna.WPF.ApplicationFramework;
using XWord.GUI.Presenters.Interface;
using System.Collections;
using System.Windows;
using System.Collections.Specialized;
using System.Windows.Controls;
using Microsoft.Win32;
using System.IO;
using System.Windows.Data;
using XWord.WPF.ApplicationFramework.Threads;
using XWord.Common;

namespace XWord.GUI.Presenters.Fundamental
{
   public partial class CategoryExplorerPresenter
    {
        #region word action

        private object GetDataContext(object obj)
        {
            var element = obj as FrameworkElement;
            if (element != null) return element.DataContext;
            return (obj as FrameworkContentElement).DataContext;
        }

        public void AddWord(DragEventArgs args, IDataObject obj)
        {
            var dc = GetDataContext(args.OriginalSource);
            if (dc == null) return;

            var ht = ((DictionaryEntry)dc).Value as Hashtable;
            var str = Convert.ToString(obj.GetData(DataFormats.UnicodeText));
            if (string.IsNullOrEmpty(str)) return;

            var sc = GetStrings(ht);
            if (!sc.Contains(str))
            {
                sc.Add(str);
                Refresh();
            }
        }

        private void DeleteWord(string word)
        {
            SelectedItemStrings.Remove(word);
            WordExplorerPresenter.WordList = SelectedItemStrings.Cast<string>().ToObservableCollection();
            Refresh();
        }

        private StringCollection GetStrings(Hashtable ht)
        {
            if (ht["List"] == null) ht["List"] = new StringCollection();
            return ht["List"] as StringCollection;
        }

        private StringCollection SelectedItemStrings
        {
            get
            {
                var ht = SelectedItem.Value as Hashtable;
                if (ht["List"] == null) ht["List"] = new StringCollection();
                return ht["List"] as StringCollection;
            }
        }

        public void EmportWord()
        {
            FileDialog dialog = new OpenFileDialog();
            var result = dialog.ShowDialog();
            if (!result.Value) return;

            var list = SelectedItemStrings;

            string line = string.Empty;

            using (TextReader streamReader =
                new StreamReader(dialog.SafeFileName, Encoding.UTF8))
            {
                while ((line = streamReader.ReadLine()) != null)
                {
                    string name = line.Trim();
                    var word = WordExplorerPresenter.AllWordList.FirstOrDefault(e => e.Trim() == name);
                    if (!string.IsNullOrEmpty(word) && !list.Contains(word))
                    {
                        list.Add(word);
                    }
                }
            }
            Refresh();
        }

        #endregion

        #region category action

        public void OpenNewDialog()
        {
            OpenDialog(true);
        }

        public void OpenEditDialog()
        {
            OpenDialog(false);
        }

        private void OpenDialog(bool IsNew)
        {
            var presenter = _serviceLocator.GetInstance<CategoryEditPresenter>();

            //this.ActiveScreen = presenter;
            if (IsNew)
            {
            }
            else
            {
                presenter.SelectedItem = SelectedItem.Key.ToString();
            }
            _windowManager
                .ShowDialog(presenter, null, ExecuteShutdownModel);
            if (presenter.IsCanceled) return;
            if (IsNew)
            {
                var ht = new Hashtable();
                ht["List"] = new StringCollection();
                this.CategoryList.Add(presenter.SelectedItem, ht);

                Refresh();
            }
            else
            {
                if (presenter.SelectedItem != SelectedItem.Key.ToString())
                {
                    this.CategoryList[presenter.SelectedItem] = SelectedItem.Value;

                    this.CategoryList.Remove(SelectedItem.Key.ToString());

                    Refresh();
                }
            }
        }

        public void Delete()
        {
            CategoryList.Remove(SelectedItem.Key);

            Refresh();
            WordExplorerPresenter.WordList.Clear();
        }

        #endregion
    }
}
